<template>
  <div class="app-container">
    <el-form :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      class="form-search"
      label-width="120px">
      <el-form-item :label="$t('merchant.dateRange')">
        <el-date-picker v-model="dateRange"
          value-format="yyyy-MM-dd"
          type="daterange"
          :clearable="false"
          range-separator="-"
          :start-placeholder="$t('common.startDate')"
          :end-placeholder="$t('common.endDate')"
          style="width: 240px"></el-date-picker>
      </el-form-item>

      <el-form-item :label="$t('merchant.exchangeName')"
        prop="exchangeName">
        <el-select v-model="queryParams.exchangeId"
          filterable
          remote
          reserve-keyword
          clearable
          :placeholder="$t('common.placeholderSelect')"
          :remote-method="remoteMethod"
          :loading="exhloading"
          style="width: 240px">
          <el-option v-for="item in exchangeList"
            :key="item.exchangeId"
            :label="item.exchangeName"
            :value="item.exchangeId">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery">{{$t('common.search')}}</el-button>
        <el-button icon="el-icon-refresh"
          size="mini"
          @click="resetQuery">{{$t('common.reset')}}</el-button>
      </el-form-item>
    </el-form>

    <div class="section-box">
      <div class="section-title">{{$t('merchant.merchantStatistics')}}</div>
      <div class="statistics-data">
        <div class="item-data">
          <div class="item-data-type">{{$t('common.payIn')}}</div>
          <div class="item-data-num">
            <div class="item-data-num-text">{{sumData.payIn || 0}}</div>
            <div class="item-data-num-unit">{{$t('common.unit')}}</div>
          </div>
        </div>
        <div class="item-data">
          <div class="item-data-type">{{$t('common.payOut')}}</div>
          <div class="item-data-num">
            <div class="item-data-num-text">{{sumData.payOut || 0}}</div>
            <div class="item-data-num-unit">{{$t('common.U')}}</div>
          </div>
        </div>
        <div class="item-data">
          <div class="item-data-type">{{$t('common.payIn')}}</div>
          <div class="item-data-num">
            <div class="item-data-num-text">{{sumData.payInTimes || 0}}</div>
            <div class="item-data-num-unit">{{$t('common.per')}}</div>
          </div>
        </div>
        <div class="item-data">
          <div class="item-data-type">{{$t('common.payOut')}}</div>
          <div class="item-data-num">
            <div class="item-data-num-text">{{sumData.payOutTimes || 0}}</div>
            <div class="item-data-num-unit">{{$t('common.per')}}</div>
          </div>
        </div>
      </div>
    </div>

    <div class="section-box">
      <div class="section-title">{{$t('merchant.merchantDetail')}}</div>
      <el-table v-loading="loading"
        :data="detailList">
        <el-table-column :label="$t('merchant.exchangeName')"
          align="center"
          prop="exchangeName"
          min-width="160"
          show-overflow-tooltip />
        <el-table-column :label="$t('merchant.merchantType')"
          align="center"
          prop="exchangeType"
          min-width="160" />

        <el-table-column :label="$t('merchant.payinINR')"
          align="center"
          prop="payIn"
          min-width="160" />
        <el-table-column :label="$t('merchant.payoutU')"
          align="center"
          prop="payOut"
          min-width="160" />
        <el-table-column :label="$t('merchant.transDate')"
          align="center"
          prop="payDate"
          min-width="160" />

        <el-table-column :label="$t('common.controls')"
          align="center"
          class-name="small-padding fixed-width"
          fixed="right"
          min-width="160">
          <template slot-scope="scope">
            <el-button size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleView(scope.row)">{{$t('common.view')}}</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>

  </div>
</template>

<script>
import {
  sumByMerchantList,
  sumByMerchant,
} from "@/api/overview/merchant.js";
import { dateFormat } from '@/utils/util.js'
import {
  getExhUserList,
} from "@/api/system/exchangeUser.js";


export default {
  name: "Merchant",
  data () {
    return {
      // 遮罩层
      loading: true,
      // 选中数组

      // 总条数
      total: 0,
      // 角色表格数据
      detailList: [],
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        startDate: null,
        endDate: null,
        exchangeId: null,
      },
      exchangeList: [],
      exhloading: false,
      sumData: {}
    };
  },
  watch: {
    dateRange (val) {
      if (val) {
        this.queryParams.startDate = val[0]
        this.queryParams.endDate = val[1]
      } else {
        this.queryParams.startDate = null
        this.queryParams.endDate = null
      }
    },
  },
  created () {
    this.dateRange = [dateFormat(new Date(), "yyyy-MM-dd"), dateFormat(new Date(), "yyyy-MM-dd")]
    this.queryParams.startDate = dateFormat(new Date(), "yyyy-MM-dd")
    this.queryParams.endDate = dateFormat(new Date(), "yyyy-MM-dd")
    this.getList();
    this.getAccountTotal()
  },
  methods: {
    // 搜索汇兑用户
    remoteMethod (query) {
      if (query !== '') {
        this.exhloading = true;
        getExhUserList({
          pageNum: 1,
          pageSize: 10,
          payType: 'payin',
          status: 1,
          exchangeName: query
        }).then(
          (response) => {
            this.exchangeList = response.rows;
            this.exhloading = false;
          }
        );
      } else {
        this.exchangeList = [];
      }
    },
    /** 查询列表 */
    getList () {
      this.loading = true;
      sumByMerchantList(this.queryParams).then(
        (response) => {
          this.detailList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 查询total data
    getAccountTotal () {
      sumByMerchant(this.queryParams).then(
        (response) => {
          if (response.rows[0]) {
            this.sumData = response.rows[0];
          } else {
            this.sumData = {}
          }
        }
      );
    },
    // 查看详情
    handleView (row) {
      this.$router.push({
        path: "/overview/merchant-list/merchantDetail",
        query: {
          exchangeId: row.exchangeId,
          transDate: row.payDate,
          startDate: row.payDate,
          endDate: row.payDate,
          refreshTime: new Date().getTime() + ''
        }
      });
    },

    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
      this.getAccountTotal()
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.dateRange = [dateFormat(new Date(), "yyyy-MM-dd"), dateFormat(new Date(), "yyyy-MM-dd")]
      this.queryParams.startDate = dateFormat(new Date(), "yyyy-MM-dd")
      this.queryParams.endDate = dateFormat(new Date(), "yyyy-MM-dd")
      this.exchangeList = []
      this.queryParams.exchangeId = ''
      this.handleQuery();
    },

  },
};
</script>

<style lang="scss" scoped>
.section-box {
  background-color: #fff;
  margin-top: 15px;
  padding: 20px;
  .section-title {
    font-size: 18px;
    color: #606266;
    font-weight: bold;
    margin-bottom: 20px;
  }
  .statistics-data {
    display: flex;
    .item-data {
      flex: 1;
      overflow: hidden;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      padding: 10px 0;
      &:not(:last-child) {
        border-right: 1px dashed #ccc;
      }
      .item-data-type {
        font-size: 14px;
        color: #999;
        margin-bottom: 10px;
        font-weight: bold;
      }
      .item-data-num {
        display: flex;
        align-items: center;
        .item-data-num-text {
          font-size: 28px;
          color: #333;
          font-weight: bold;
        }
        .item-data-num-unit {
          font-size: 14px;
          color: #999;
          margin-left: 8px;
        }
      }
    }
  }
}
</style>
